package com.nercel.dsj.gksales.api.controller;

import com.nercel.dsj.gksales.model.QueryParams;
import com.nercel.dsj.gksales.model.dto.RoleParamsDTO;
import com.nercel.dsj.gksales.model.entity.Role;
import com.nercel.dsj.gksales.service.RoleService;
import com.nercel.dsj.gksales.util.ApiResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author sunlin
 * @date 2018-11-18
 */
@RestController
@Api(tags = "角色接口")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class RoleApiController {

    private final RoleService roleService;

    @GetMapping(value = "/api/v1/roles")
    @ApiOperation(value = "角色列表", response = Role.class)
    public List<Role> list(@ApiParam(hidden = true) QueryParams queryParams,
                                 @ApiParam(value = "查询关键词") @RequestParam(required = false) String keyword) {
        queryParams.put("keyword", keyword);
        return roleService.list(queryParams);
    }

    @PutMapping(value = "/api/v1/roles/{id}")
    @ApiOperation(value = "更新角色信息")
    public Role update(@ApiParam(value = "数据ID") @PathVariable Integer id,
                             @ApiParam(value = "角色信息") @RequestBody RoleParamsDTO role
    ) {
        role.setId(id);
        roleService.update(role);
        return roleService.findById(id);
    }

    @GetMapping(value = "/api/v1/roles/{id}")
    @ApiOperation(value = "获取角色信息", response = Role.class)
    public Object get(@ApiParam(value = "ID") @PathVariable Integer id) {
        Role role = roleService.findById(id);
        return ApiResultUtil.notNull(role);
    }

    @PostMapping(value = "/api/v1/roles")
    @ApiOperation(value = "新增角色")
    public Role create(@ApiParam(value = "角色信息") @RequestBody RoleParamsDTO role) {
        roleService.save(role);
        return null;
    }

    @DeleteMapping(value = "/api/v1/roles/{id}")
    @ApiOperation(value = "删除角色")
    public Object delete(@ApiParam(value = "角色ID") @PathVariable Integer id) {
        roleService.delete(id);
        return ApiResultUtil.success();
    }

    @PostMapping(value = "/api/v1/roles/{roleId}/permissions")
    @ApiOperation(value = "新增角色权限(批量)")
    public Object addPermissions(@ApiParam(value = "角色ID") @PathVariable Integer roleId,
                                 @ApiParam(value = "权限ID数组") @RequestBody List<Integer> ids) {
        roleService.addPermissions(roleId, ids);
        return ApiResultUtil.success();
    }

    @PutMapping(value = "/api/v1/roles/{roleId}/permissions")
    @ApiOperation(value = "批量更新角色权限(删除原有)")
    public Object updatePermissions(@ApiParam(value = "角色ID") @PathVariable Integer roleId,
                                 @ApiParam(value = "权限ID数组") @RequestBody List<Integer> ids) {
        roleService.updatePermissions(roleId, ids);
        return ApiResultUtil.success();
    }

    @DeleteMapping(value = "/api/v1/roles/{roleId}/permissions/{permissionId}")
    @ApiOperation(value = "删除角色权限(单个)")
    public Object deletePermission(@ApiParam(value = "角色ID") @PathVariable Integer roleId,
                                   @ApiParam(value = "权限ID") @PathVariable Integer permissionId) {
        roleService.deletePermission(roleId, permissionId);
        return ApiResultUtil.success();
    }


}
